package com.atguigu.boot.admin.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 *  1，MyServlet  --> /my
 *  2.DispatcherServlet  -->  /
 *
 *
 * @author : leimingchao
 * @date Date : 2021年11月20日 21:02
 */
//(proxyBeanMethods = true):保证依赖的组件始终是单实例的
@Configuration
public class MyRegisterConfig {


    @Bean
    public ServletRegistrationBean myServlet() {
        MyServlet myServlet = new MyServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(myServlet,"/my","/my02");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean myFilter() {
        MyFilter myFilter = new MyFilter();
        //方式一：
        // FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter,myServlet());
        // return filterRegistrationBean;
        //方式二：
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener() {
        MyServletContextListener myServletContextListener = new MyServletContextListener();
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(myServletContextListener);
        return servletListenerRegistrationBean;
    }
}
